package com.example.medical.controller.admin;

import com.example.medical.entity.dto.AdminSaveUserDto;
import com.example.medical.entity.dto.AdminUpdateUserDto;
import com.example.medical.entity.dto.LeadershipGetUserListDto;
import com.example.medical.service.UserService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UserService userService;

    @PostMapping("get_user_list")
    public Result AdminGetUserList(@RequestBody LeadershipGetUserListDto dto){
        return userService.LeadershipGetUserList(dto);
    }

    @GetMapping("/get_user_detail")
    public Result AdminGetUserDetail(@RequestParam("id") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(userService.getById(id));
    }

    @GetMapping("/remove_user")
    public Result AdminRemoveUser(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = userService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_user_arr")
    public Result AdminRemoveUserArr(@RequestParam("ids")String[] ids){
        return userService.AdminRemoveUserArr(ids);
    }

    @GetMapping("/update_get_user")
    public Result AdminUpdateGetUser(@RequestParam("id") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(userService.getById(id));
    }

    @PostMapping("/update_user")
    public Result AdminUpdateUser(@RequestParam("id")Long id,
                                  @RequestParam(value = "name",required = false)String name,
                                  @RequestParam(value = "nick",required = false)String nick,
                                  @RequestParam(value = "phone",required = false)Long phone,
                                  @RequestParam(value = "number",required = false)Long number,
                                  @RequestParam(value = "file",required = false)MultipartFile file,
                                  @RequestParam(value = "sex",required = false)Integer sex,
                                  @RequestParam(value = "divisionId",required = false)String divisionId,
                                  @RequestParam(value = "email",required = false)String email) throws Exception {
        return userService.AdminUpdateUser(id,name,nick,phone,number,file,sex,divisionId,email);
    }

    @GetMapping("/update_user_password")
    public Result AdminUpdateUserPassword(@RequestParam("id")Long id){
        return userService.AdminUpdateUserPassword(id);
    }

    @PostMapping("/save_user")
    public Result AdminSaveUser(@RequestParam("name")String name,
                                @RequestParam("nick")String nick,
                                @RequestParam("phone")Long phone,
                                @RequestParam("number")Long number,
                                @RequestParam("file")MultipartFile file,
                                @RequestParam("sex")Integer sex,
                                @RequestParam("divisionId")Long divisionId,
                                @RequestParam("email")String email) throws Exception{
        return userService.AdminSaveUser(name,nick,phone,number,file,sex,divisionId,email);
    }

}
